//
//  QueryViewController.m
//  PropertyMananger
//
//  Created by bh on 16/12/22.
//  Copyright © 2016年 BH. All rights reserved.
//

#import "QueryViewController.h"
#import "QueryMenuCell.h"

#import "RepairController.h"
#import "ParkingController.h"
#import "ProprertyPayController.h"
#import "OwnweController.h"
#import "HouseInfoQueryController.h"
#import "LeaderController.h"
#import "FormQueryController.h"

static NSString * cellID = @"QueryCellID";

@interface QueryViewController ()<UITableViewDelegate,UITableViewDataSource,CarouselDataSource,CarouselDelegate>
{
    /**轮播图**/
    CarouselControl * carouselView;
}
@property (nonatomic,strong) UITableView * mTableView;
@property (nonatomic,strong) NSString * buiddingID;
/**工具菜单数据源**/
@property (nonatomic, strong) NSMutableArray * menuArray;
@property (nonatomic,strong)  NSArray *imagesURL;

@end

@implementation QueryViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self initData];
    [self createInfiniteScrollView];
    [self createTableViewUI];
    
    [self postFloorQueryData];
    
    [self.navigationController.navigationBar setTitleTextAttributes:
     @{NSFontAttributeName:[UIFont systemFontOfSize:19],
       NSForegroundColorAttributeName:[UIColor whiteColor]}];
    
    self.automaticallyAdjustsScrollViewInsets = NO;
    self.edgesForExtendedLayout = UIRectEdgeNone;
}
#pragma mark - 初始化数据源
- (void)initData
{
    NSString *plistPath = [[NSBundle mainBundle]pathForResource:@"QueryMenuData" ofType:@"plist"];
    self.menuArray = [[NSMutableArray alloc]initWithContentsOfFile:plistPath];
}
#pragma mark - 创建顶部的轮播图
- (void)createInfiniteScrollView {
    
    _imagesURL =  @[@{@"image":[UIImage imageNamed:@"banner04"]}];
    
    carouselView = [[CarouselControl alloc]initWithFrame:CGRectMake(0, 0, BHscreenW, 165)];
   // carouselView.carouseScrollTimeInterval = 2.0f;
    carouselView.dataSource = self;
    carouselView.delegate = self;
    [self.view addSubview:carouselView];
    if (_imagesURL.count == 1) {
        
        carouselView.coreView.scrollEnabled = NO;
    }
    /*
     *   必须实现下面重载数据方法
     */
    
    [carouselView reloadData];
    
}
#pragma mark --- dataSource
-(NSArray *) zzcarousel:(CarouselControl *)carouselView
{
    return _imagesURL;
}

-(UIView *) zzcarousel:(CarouselControl *)carouselView carouselFrame:(CGRect)frame data:(NSArray *)data viewForItemAtIndex:(NSInteger)index
{
    /*
     *   此方法中必须返回UIView
     *   注意在此方法中创建 UI 控件时，必须使用此方法冲的frame参数来设定位置。
     *   注意在此方法中的数据源必须从data参数中获取。
     */
//    UIView *view = [[UIView alloc]initWithFrame:frame];
//    UIImageView *imageView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, frame.size.width, frame.size.height)];
//    [imageView sd_setImageWithURL:[NSURL URLWithString:[data objectAtIndex:index]]];
//    [view addSubview:imageView];
    
    //本地图片
    UIView *view = [[UIView alloc]initWithFrame:frame];
    UIImageView *imageView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, frame.size.width, frame.size.height)];
    imageView.image = [[data objectAtIndex:index] objectForKey:@"image"];
    [view addSubview:imageView];
    return view;
}

#pragma mark --- 广告轮播代理方法,点击跳转广告

-(void)zzcarouselView:(CarouselControl *)zzcarouselView didSelectItemAtIndex:(NSInteger)index
{
    NSLog(@"点击了 %lu",index);
}

#pragma mark - 初始化tableview
- (void)createTableViewUI
{
    self.mTableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 165, BHscreenW, BHscreenH -44) style:UITableViewStylePlain];
    self.mTableView.delegate = self;
    self.mTableView.dataSource = self;
    self.mTableView.tableFooterView = [[UIView alloc] initWithFrame:CGRectZero];
   // self.mTableView.tableHeaderView = carouselView;
    self.mTableView.backgroundColor = BHGrayColor;
    [self.view addSubview:self.mTableView];
    //注册
    
}
#pragma makr - 轮播图点击
- (void)ScrollViewDidClickAtAnyImageView:(UIImageView *)imageView {
    
    NSLog(@"点击了%ld张",imageView.tag);
    
}
#pragma mark - 代理方法
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    
    return 1;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    QueryMenuCell * menuCell = [QueryMenuCell cellWithTableView:tableView menuArray:self.menuArray];
    menuCell.queryBlock = ^(NSInteger index){
    
        DLog(@"点击查询==%ld",index);
        if (index == ChaoBiaoQuery) {
         //抄表查询
        FormQueryController * fromQueryVC = [[FormQueryController alloc]init];
        fromQueryVC.buidUnitID = self.buiddingID;
        [self.navigationController pushViewController:fromQueryVC animated:YES];
            
        }else if (index == RepairQuery){
        //报修单查询
            RepairController * repairVC = [[RepairController alloc]init];
            [self.navigationController pushViewController:repairVC animated:YES];
        
        }else if (index == ParkingQuery){
        //停车费管理
            ParkingController * parkingVC = [[ParkingController alloc]init];
            
            [self.navigationController pushViewController:parkingVC animated:YES];
        
        }else if (index == PaymentQuery){
        //缴费管理
            ProprertyPayController * propretyPayVC = [[ProprertyPayController alloc]init];
            [self.navigationController pushViewController:propretyPayVC animated:YES];
        
        }else if (index == OwnerInfoQuery){
        //业主信息
            OwnweController * ownerVC = [[OwnweController alloc]init];
            
            [self.navigationController pushViewController:ownerVC animated:YES];
        
        }else if (index == HouseInfoQuery){
        //房屋信息查询
            HouseInfoQueryController * houseInfoVC = [[HouseInfoQueryController alloc]init];
            
            houseInfoVC.buiddingID = self.buiddingID;
            
            [self.navigationController pushViewController:houseInfoVC animated:YES];
        
        }else if (index == LeaderFrom){
        //领导报表
            LeaderController * leaderVC = [[LeaderController alloc]init];
            [self.navigationController pushViewController:leaderVC animated:YES];
        
        }
    
    };
    return menuCell;
    
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 168;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 0.0001f;
}
#pragma mark - 获取楼栋信息
- (void)postFloorQueryData
{
    NSDictionary * parms = @{
                             @"CommunityID":userModel.CommunityID,
                             @"json":@"\{\"jsonEntity\":\{},\"action\":\"getbuilding\",\"version\":\"V1.0\"}"};
    [HomeNetWorkRequest postFloorQuerySuccess:^(id data) {
        
        NSDictionary * dict = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
        
        NSString * str = [NSString stringWithFormat:@"%@",dict[@"Data"]];
        NSData * data2 = [str dataUsingEncoding:NSUTF8StringEncoding];
        NSArray * array = [NSJSONSerialization JSONObjectWithData:data2 options:NSJSONReadingMutableContainers error:nil];
        //DLog(@"楼栋信息的数据 == %@",array);
        
        NSDictionary *dic = array.firstObject;
        self.buiddingID = dic[@"BuildUnitId"];
        DLog(@"第一个id == %@",self.buiddingID);
        
    } Fail:^(NSString *errorDes) {
        
    } param:parms];
    
}

@end
